using System;
using System.Drawing;
using System.Windows.Forms;
using AocBossTimer.Timer;

namespace AocBossTimer
{
    public partial class formAocBossTimer : Form, ITimerConfigurable
    {
        private IBossTimer currentTimer = new VistrixTimer();
        private TimerState timerState;

        public formAocBossTimer()
        {
            InitializeComponent();
            Initialize();
        }

        #region ITimerConfigurable Members

        public IBossTimer Timer
        {
            get { return currentTimer; }
            set
            {
                currentTimer = value;
                Initialize();
            }
        }

        public int Fontsize
        {
            get { return Convert.ToInt32(lblTimer.Font.Size); }
            set { lblTimer.Font = new Font(lblTimer.Font.FontFamily, value); }
        }

        #endregion

        private void Initialize()
        {
            SetTimerRunIndicatorStopped();
            InitializeLblTimer();
        }

        private void InitializeLblTimer()
        {
            SetLblTimer(currentTimer.StartValue);
            timer.Enabled = false;
        }

        private void formAocBossTimer_KeyPress(object sender, KeyPressEventArgs e)
        {
            KeyPressed(e);
        }

        private void KeyPressed(KeyPressEventArgs e)
        {
            if (e.KeyChar == (char) Keys.Back)
            {
                ConfigForm f = new ConfigForm(currentTimer, this);
                f.Show(this);
            }
            else if (e.KeyChar == (char) Keys.Escape)
            {
                timer.Enabled = false;
                if (timerState == TimerState.Stopped)
                {
                    ResetTimer();
                }
                SetTimerRunIndicatorStopped();
            }
            else
            {
                if (timerState == TimerState.Running)
                {
                    ResetTimer();
                }
                timer.Enabled = true;
                SetTimerRunIndicatorRunning();
            }
        }

        private void ResetTimer()
        {
            currentTimer.Reset();
            InitializeLblTimer();
        }

        private void SetTimerRunIndicatorRunning()
        {
            lblRunning.ForeColor = Color.Green;
            lblRunning.Text = string.Format("Running {0}", currentTimer.Name);
            timerState = TimerState.Running;
        }

        private void SetTimerRunIndicatorStopped()
        {
            lblRunning.ForeColor = Color.Red;
            lblRunning.Text = string.Format("Stopped {0}", currentTimer.Name);
            timerState = TimerState.Stopped;
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            TickResult result = currentTimer.Tick();
            SetLblTimer(result);
        }

        private void SetLblTimer(TickResult result)
        {
            lblTimer.Text = result.Seconds.ToString();
            lblTimer.ForeColor = result.Color;
        }

        #region Nested type: TimerState

        private enum TimerState
        {
            Running,
            Stopped
        }

        #endregion
    }
}